package com.datviet.service.common.security;

import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.datviet.entity.User;

public class UserDetailsImpl implements UserDetails {	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;	
	
    /* Spring Security fields*/
    private List<GrantedAuthorityImpl> authorities;
    
    private final User user;
    
    public UserDetailsImpl(User user){
    	this.user = user;
    }
    public User getUser() {
		return user;
	}
    
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}
	
	public void setAuthorities(List<GrantedAuthorityImpl> authorities) {
		this.authorities = authorities;
	}

	@Override
	public String getPassword() {
		if(user != null)
			return user.getPassword();
		return "";
	}
	
	@Override
	public String getUsername() {
		if(user != null)
			return user.getUsername();
		return "";
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}	

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}
	
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}
	
	@Override
	public boolean isEnabled() {
		return true;
	}
	
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("User [username=");
        builder.append(getUsername());
        builder.append(", password=");
        builder.append(getPassword());
        builder.append(", firstName=");
        builder.append(", authorities=");
        builder.append(authorities);
        builder.append(", accountNonExpired=");
        builder.append(isAccountNonExpired());
        builder.append(", accountNonLocked=");
        builder.append(isAccountNonLocked());
        builder.append(", credentialsNonExpired=");
        builder.append(isCredentialsNonExpired());
        builder.append(", enabled=");
        builder.append(isEnabled());
        builder.append("]");
        return builder.toString();
    }

}
